package org.infodavid.common.model;

import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.joda.time.DateTime;

import org.infodavid.common.IBuilder;
import org.infodavid.common.dto.IPersonListItem.EPersonTitle;
import org.infodavid.common.ejb.PersonBean;
import org.infodavid.common.model.components.IEMail;
import org.infodavid.common.model.components.ILocation;
import org.infodavid.common.model.components.IPhone;
import org.infodavid.common.model.components.IUri;

/**
 * The Class PersonTest.
 */
public class PersonTest extends AbstractBeanTest<IPerson> {

	/**
	 * Instantiates a new person test.
	 * @param mainTest the main test
	 */
	public PersonTest(final AbstractPersistenceTest mainTest) {
		super(mainTest);
	}

	/**
	 * Instantiates a new person test.
	 */
	public PersonTest() {
		super();
	}

	/**
	 * Populate.
	 * @param count the count
	 * @return the collection< test data>
	 * @throws Exception the exception
	 */
	@Override
	protected List<IPerson> doCreate(final int count) throws Exception {
		final IBuilder<IPerson> builder = ModelFactory.getInstance().getBuilder(IPerson.class);
		final List<IPerson> items = new ArrayList<IPerson>(count);
		final Iterator<ILocation> locations = TestUtil.createLocations(count).iterator();
		final IBuilder<IEMail> emailBuilder = ModelFactory.getInstance().getBuilder(IEMail.class);
		final IBuilder<IUri> uriBuilder = ModelFactory.getInstance().getBuilder(IUri.class);
		final IBuilder<IPhone> phoneBuilder = ModelFactory.getInstance().getBuilder(IPhone.class);
		final IBuilder<IImageData> imgBuilder = ModelFactory.getInstance().getBuilder(IImageData.class);
		final byte[] bytes = new byte[1024];
		IPerson item;
		IEMail email;
		IPhone phone;
		IUri uri;
		long key;

		for (int i = 0; i < count; i++) {
			item = builder.create();
			key = System.nanoTime();
			
			item.setName("item-" + key);
			item.setFirstName("firstname-" + key);
			item.setComment("comment-" + key);
			item.setIdentity("identity-" + key);
			item.setNationality("FR");
			item.setImage(imgBuilder.create());
			RAND.nextBytes(bytes);
			item.getImage().setValue(bytes);
			item.getImage().setType(
			    IImageData.EImageType.values()[RAND.nextInt(IImageData.EImageType.values().length)]);
			item.setTitle(EPersonTitle.values()[RAND.nextInt(EPersonTitle.values().length)]);
			item.setBirthDate(new DateTime());
			item.setDeathDate(new DateTime());

			item.getLocations().add(locations.next());

			email = emailBuilder.create();

			email.setValue(item.getName() + "@domain.com");
			item.getEmails().add(email);

			uri = uriBuilder.create();

			uri.setValue(new URI("http://www.darpa.mil/" + key));
			item.getWebSites().add(uri);

			phone = phoneBuilder.create();

			phone.setValue("0123456789");
			item.getPhones().add(phone);

			item.getProperties().addData("key1", "value1");
			item.getProperties().addData("key2", "value2");

			items.add(item);
		}

		return items;
	}

	/**
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.AbstractBeanTest#getEntityClass()
	 */
	@Override
	public Class<PersonBean> getEntityClass() {
		return PersonBean.class;
	}

	/**
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.AbstractBeanTest#getEntityName()
	 */
	@Override
	public String getEntityName() {
		return IPerson.class.getSimpleName();
	}
}
